package com.lenovo.agricultureclient.request;

import org.json.JSONException;
import org.json.JSONObject;

import com.lenovo.agricultureclient.util.Util;
/**
 * 设置所有传感器阀值的请求类
 * @author zongbingwu
 *
 */
public class SetConfigRequest extends AgricultureRequest 
{
	//action name
	private static final String ACTION = "setConfig"; 
	
	private int controlAuto = 0; //0表示关闭，1表示打开
	
	private int maxPM25 = 0;//PM2.5浓度最大值
	
	private int minCo2 = 0;//CO2浓度最小值
	private int maxCo2 = 0;//CO2浓度最大值
	
	private int minLight = 0;//灯光强度最小值
	private int maxLight = 0;//灯光强度最大值
	
	private int minAirTemperature = 0;//空气温度最小值
	private int maxAirTemperature = 0;//空气温度最大值
	private int minAirHumidity = 0;//空气湿度最小值
	private int maxAirHumidity = 0;//空气湿度最大值
	
	private int minSoilTemperature = 0;//土壤温度最小值
	private int maxSoilTemperature = 0;//土壤温度最大值
	private int minSoilHumidity = 0;//土壤湿度最小值
	private int maxSoilHumidity = 0;//土壤湿度最大值
	
	//设置选项开关
	private boolean isSetAutoControl = false;
	private boolean isSetPm25 = false;
	private boolean isSetCo2 = false;
	private boolean isSetLight = false;
	private boolean isSetAir = false;
	private boolean isSetSoil = false;
	
	@Override
	protected String getActionName() {
		return ACTION;
	}
	
	//jason协议时的获取body函数
	@Override
	protected String onGetJasonBody() 
	{
		JSONObject jsonObj = new JSONObject();
		try 
		{
			//设置自动控制模式
			if(isSetAutoControl){
				jsonObj.put("controlAuto", controlAuto);
			}
			//设置PM2.5浓度最大值
			if(isSetPm25){
				jsonObj.put("maxPM25", maxPM25);
			}
			//设置CO2浓度最小值和最大值
			if(isSetCo2){
				jsonObj.put("minCo2", minCo2);
				jsonObj.put("maxCo2", maxCo2);
			}
			//设置灯光强度最小值和最大值
			if(isSetLight){
				jsonObj.put("minLight", minLight);
				jsonObj.put("maxLight", maxLight);
			}
			
			if(isSetAir){
				//设置空气温度最小值和最大值
				jsonObj.put("minAirTemperature", minAirTemperature);
				jsonObj.put("maxAirTemperature", maxAirTemperature);
				//设置空气湿度最小值和最大值
				jsonObj.put("minAirHumidity", minAirHumidity);
				jsonObj.put("maxAirHumidity", maxAirHumidity);
			}
			if(isSetSoil){
				//设置土壤温度最小值和最大值
				jsonObj.put("minSoilTemperature", minSoilTemperature);
				jsonObj.put("maxSoilTemperature", maxSoilTemperature);
				//设置土壤湿度最小值和最大值
				jsonObj.put("minSoilHumidity", minSoilHumidity);
				jsonObj.put("maxSoilHumidity", maxSoilHumidity);
			}
			return jsonObj.toString();
		} 
		catch (JSONException e) {
			e.printStackTrace();
		}
		return super.onGetJasonBody();
	}
	
	//soap协议时的获取body函数
	@Override
	protected String onGetSoapBody() 
	{
		String soapBody = "";
		//设置自动控制模式
		if(isSetAutoControl){
			soapBody += Util.getXmlElementStr("controlAuto", controlAuto+"");
		}
		//设置PM2.5浓度最大值
		if(isSetPm25){
			soapBody += Util.getXmlElementStr("maxPM25", maxPM25+"");
		}
		//设置CO2浓度最小值和最大值
		if(isSetCo2){
			soapBody += Util.getXmlElementStr("minCo2", minCo2+"");
			soapBody += Util.getXmlElementStr("maxCo2", maxCo2+"");
		}
		//设置灯光强度最小值和最大值
		if(isSetLight){
			soapBody += Util.getXmlElementStr("minLight", minLight+"");
			soapBody += Util.getXmlElementStr("maxLight", maxLight+"");
		}
		if(isSetAir){
			//设置空气温度最小值和最大值
			soapBody += Util.getXmlElementStr("minAirTemperature", minAirTemperature+"");
			soapBody += Util.getXmlElementStr("maxAirTemperature", maxAirTemperature+"");
			//设置空气湿度最小值和最大值
			soapBody += Util.getXmlElementStr("minAirHumidity", minAirHumidity+"");
			soapBody += Util.getXmlElementStr("maxAirHumidity", maxAirHumidity+"");
		}
		if(isSetSoil){
			//设置土壤温度最小值和最大值
			soapBody += Util.getXmlElementStr("minSoilTemperature", minSoilTemperature+"");
			soapBody += Util.getXmlElementStr("maxSoilTemperature", maxSoilTemperature+"");
			//设置土壤湿度最小值和最大值
			soapBody += Util.getXmlElementStr("minSoilHumidity", minSoilHumidity+"");
			soapBody += Util.getXmlElementStr("maxSoilHumidity", maxSoilHumidity+"");
		}
		return Util.getSoapXml(soapBody);
	}
	//设置是否为自动控制
	public void setAutoControl(int autoControl) {
		this.controlAuto = autoControl;
		this.isSetAutoControl = true;
	}
	//设置PM2.5浓度阀值
	public void setPm25(int maxPM25) {
		this.maxPM25 = maxPM25;
		this.isSetPm25 = true;
	}
	//设置CO2浓度阀值
	public void setCo2(int minCo2, int maxCo2) {
		this.minCo2 = minCo2;
		this.maxCo2 = maxCo2;
		this.isSetCo2 = true;
	}
	//设置光照强度
	public void setLight(int minLight, int maxLight) {
		this.minLight = minLight;
		this.maxLight = maxLight;
		this.isSetLight = true;
	}
	//设置空气温湿度
	public void setAir(int minAirTemperature, int maxAirTemperature, int minAirHumidity, int maxAirHumidity) {
		this.minAirTemperature = minAirTemperature;
		this.maxAirTemperature = maxAirTemperature;
		this.minAirHumidity = minAirHumidity;
		this.maxAirHumidity = maxAirHumidity;
		this.isSetAir = true;
	}
	
	//设置土壤温湿度
	public void setSoil(int minSoilTemperature, int maxSoilTemperature, int minSoilHumidity, int maxSoilHumidity) {
		this.minSoilTemperature = minSoilTemperature;
		this.maxSoilTemperature = maxSoilTemperature;
		this.minSoilHumidity = minSoilHumidity;
		this.maxSoilHumidity = maxSoilHumidity;
		this.isSetSoil = true;
	}

	//所有开关重置
	public void resetRequest()
	{
		isSetAutoControl = false;
		isSetPm25 = false;
		isSetCo2 = false;
		isSetLight = false;
		isSetAir = false;
		isSetSoil = false;
	}

}
